package com.liveHere.www.dao;

import com.liveHere.www.Exception.AdminDaoException;
import com.liveHere.www.po.*;
import com.liveHere.www.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Mo312
 */
public class AdminDaoImpl implements AdminDao {
    // 管理员登录
    @Override
    public Admin login(String adminName, String password) throws SQLException {
        String sql = "SELECT * FROM admin WHERE admin_name = ? AND password = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, adminName);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Admin admin = new Admin();
                admin.setAdminId(rs.getInt("administrator_id"));
                admin.setAdminName(rs.getString("admin_name"));
                admin.setPassword(rs.getString("password"));
                return admin;
            }
        } catch (SQLException e) {
            throw new AdminDaoException("登录失败: " + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
        return null;
    }


}
